package com.tianpengtech.common.plugin.im;

import java.io.IOException;

import org.tio.server.AioServer;
import org.tio.server.ServerGroupContext;
import org.tio.server.intf.ServerAioHandler;
import org.tio.server.intf.ServerAioListener;

import com.jfinal.plugin.IPlugin;
import com.tianpengtech.common.im.common.ImPacket;
import com.tianpengtech.common.im.common.ImSessionContext;
import com.tianpengtech.common.im.kit.PushKit;
import com.tianpengtech.common.im.server.ImServerAioHandler;
import com.tianpengtech.common.im.server.ImServerAioListener;

public class ImStartPlugin implements IPlugin{

	 ServerGroupContext<ImSessionContext, ImPacket, Object> serverGroupContext = null;

	 AioServer<ImSessionContext, ImPacket, Object> aioServer = null;

	 ServerAioHandler<ImSessionContext, ImPacket, Object> aioHandler = null;

	 ServerAioListener<ImSessionContext, ImPacket, Object> aioListener = null;

	 String ip = null;//"127.0.0.1";

	 int port = 9322;

	@Override
	public boolean start() {
		aioHandler = new ImServerAioHandler();
		aioListener = new ImServerAioListener();
		serverGroupContext = new ServerGroupContext<>(aioHandler, aioListener);
		serverGroupContext.setEncodeCareWithChannelContext(true);
//		serverGroupContext.setReadBufferSize(2048);
		aioServer = new AioServer<>(serverGroupContext);
		try {
			PushKit.serverGroupContext=serverGroupContext;
			aioServer.start(ip, port);
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public boolean stop() {
		aioServer.stop();
		//System.exit(0);
		return true;
	}
}
